package main

import (
	"gitee.com/wu-jin-feng/kinfu/fgin"
	"github.com/gin-gonic/gin"
)

type UserInput struct {
	Data    []UserItem            `json:"data" binding:"dive"` // dive可以递归去检测[]嵌套结构体总binding，针对切片、map有效。如果是结构体，则单纯使用子结构体的规则
	DataMap map[string]SchoolItem `json:"data_map" binding:"dive"`
}

type UserItem struct {
	Name   string       `json:"name" binding:"required" msg:"name必填"`
	Age    int          `json:"age" binding:"required" msg:"age必填"`
	School []SchoolItem `json:"school" binding:"dive" msg:"学校信息必填"`
}

type SchoolItem struct {
	SchoolName string `json:"name" binding:"required" msg:"学校名必填"`
	Class      string `json:"class" binding:"required" msg:"班级名必填"`
}

func main() {
	f := fgin.Fgin{}
	r := f.GetHttpServer("./test.yaml")
	r.POST("/user", func(ctx *gin.Context) {
		input := UserInput{}
		if err := fgin.FbindParams(ctx, &input); err != nil {
			fgin.ReturnJson(ctx, 400, "", err)
			return
		}
		fgin.ReturnJson(ctx, 200, input, nil)
	})
	f.Run(r)
}
